package com.ruoyi.controller.targets;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.SysParameterService;
import com.ruoyi.service.targets.TargetInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/targetInfo")
public class TargetInfoController extends BaseController {

    @Autowired
    TargetInfoService targetInfoService;

    @Autowired
    SysParameterService sysParameterService;

    private String prefix = "targets";

    @GetMapping()
    public String personnel(ModelMap mmp) {
        mmp = getModelMap(mmp);
        return prefix + "/TargetInfo";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> map) {
        return getDataTable(targetInfoService.findTargetInfoAll(map));
    }

    @GetMapping("/add")
    public String add(ModelMap mmp) {
        mmp = getModelMap(mmp);
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map<String, Object> map) {
        return toAjax(targetInfoService.addTargetInfo(map));
    }

    @GetMapping("/edit/{targetId}")
    public String edit(@PathVariable("targetId") String targetId, ModelMap mmp) {
        mmp = getModelMap(mmp);
        mmp.put("targetInfo", targetInfoService.findTargetInfo(targetId));
        return prefix + "/edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map<String, Object> map) {
        return toAjax(targetInfoService.editTargetInfo(map));
    }

    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(targetInfoService.deleteTargetInfo(ids));
    }

    private ModelMap getModelMap(ModelMap mmp) {
        mmp.put("targetType", sysParameterService.findNameByType("指标类型"));
        mmp.put("targetLevel", sysParameterService.findNameByType("指标级别"));
        mmp.put("targetProducers", sysParameterService.findNameByType("指标生产方"));
        return mmp;
    }
}
